const nodemailer = require('nodemailer');
require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` });//使用dotenv库来加载这些env变量

let transporter = nodemailer.createTransport({
  host: 'smtp.163.com',
  port: 465,
  secure: true, 
  auth: {
    user: process.env.EMAIL,
    pass: process.env.EMAIL_PASS
  },
});

async function sendEmail(to, subject, text, code) {
  let mailOptions = {
    from: process.env.EMAIL,
    to: to,
    subject: subject,
    // text: text,
    // text: content,
    html: `<p>${text}</p><h1 style='color:blue'> ${code} </h1>`
  };


  try {
      await transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
          return console.log(error);
        }
        console.log('Message sent: %s', info.messageId);
      });
  } catch (error) {
    console.error('Error sending email: %s', error.message);
  }
  
}

module.exports = sendEmail;